﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.GUI.Common;
using SiGeSIB.Data.Security;
using SiGeSIB.Services;
using SiGeSIB.GUI.Common.DedaGrid.Utils;
using SiGeSIB.GUI.Common.DedaGrid.Filter;
using GUI.Security;
using SiGeSIB.Data.Common;

namespace SiGeSIB.GUI.Security
{
#if DESIGN
	public partial class FManageUsers : FormRedirector.FManageUsers
#else
	public partial class FManageUsers : FGenericManager<User>
#endif
	{
		#region Column Names
		private const string DT_NAME = "Name";
		private const string DT_LOGIN = "Login";
		private const string DT_EMAIL = "Email";
		#endregion

		#region Constructors
		public FManageUsers()
		{
			InitializeComponent();
			this.Text = "Gerenciamento de usuários";
		} 
		#endregion

		#region Properties
		protected override string _Description
		{
			get { return "Usuários"; }
		}
		#endregion

		#region EventHandlers
		protected override void uManageDefaultToolStrip_AddClicked(object sender, EventArgs e)
		{
			Manager.Instance.CreateParentTab(this, new FEditUser(EFormState.Edit, -1));
		}

		protected override void uManageDefaultToolStrip_EditClicked(object sender, EventArgs e)
		{
			DataRow row = uEntitiesGrid.GetSelectedRow();
			int currentId = (int)row[DT_ID];
			Manager.Instance.CreateParentTab(this, new FEditUser(EFormState.View, currentId));
		}
		#endregion

		#region Methods
		#region Loading
		protected override void _InitializeDataColumns()
		{
			this._DedaColumns = new List<DedaGridColumn>()
			{
				new DedaGridColumn(DT_ID, typeof(int)) { Visible = false},
				new DedaGridColumn(DT_NAME, typeof(string)),
				new DedaGridColumn(DT_LOGIN, typeof(string)),
				new DedaGridColumn(DT_EMAIL, typeof(string)),
			};
		}

		protected override object[] _ConvertToDataRow(User obj)
		{
			return
				new object[]
					{
						obj.Id,
						obj.Name,
						obj.Login,
						obj.Email
					};
		}

		protected override List<User> _LoadEntity()
		{
			ISecurityServicesProvider secProv = Manager.Instance.ServicesProvider.CreateSecurityServicesProvider();
			List<User> users = secProv.GetUsers();
			return users;
		} 
		#endregion

		protected override void _ApplyFilter(string p)
		{
			DedaGridFilterCollection collection = new DedaGridFilterCollection();
			collection.Add(new DedaGrigFilterCondition(_DtTable.Columns[DT_NAME], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_EMAIL], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_LOGIN], ELogicalCondition.CONTAINS, p));

			_DtTable.Filter(collection);
		}

		protected override bool _RequestRemove()
		{
			return MessageBox.Show(this, "Deseja remover o usuário selecionado?", "Confirmação", MessageBoxButtons.OKCancel) == DialogResult.OK;
		}

		protected override void _RemoveEntity(int id)
		{
			if (_Manager.LoggedUser.Id != id)
			{
				ISecurityServicesProvider secProv = _Manager.ServicesProvider.CreateSecurityServicesProvider();

				secProv.RemoveUser(id);
			}
			else
				throw new ValidationError("InvalidOperation", "Não é possível remover o usuário atualmente logado.");
		} 
		#endregion

		#region EventHandlers
		private void tsmiPermissions_Click(object sender, EventArgs e)
		{
			DataRow row = uEntitiesGrid.GetSelectedRow();
			int currentId = (int)row[DT_ID];
			_Manager.CreateParentTab(this, new FEditPermission(currentId));
		}

		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{
			DataRow row = uEntitiesGrid.GetSelectedRow();
			if (row == null)
				e.Cancel = true;
		} 
		#endregion
	}
}
